package com.zzsxy.mJFram.mJLable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Tips {
    public static void main(String[] args) {
        JFrame frame = new JFrame("事件监听");
        frame.setSize(500,500);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setLayout(null);

        JButton button = new JButton("按钮");
        button.setBounds(50,50,100,100);
        frame.getContentPane().add(button);

        //添加监听
        button.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("动作监听，鼠标点击/点击空格");
            }
        });
        //取消按钮的焦点（按钮创建以后，自动获得系统的焦点）
        button.setFocusPainted(false);

        button.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {
                //对于键盘的 Fn esc 上下左右 等按键无响应
            }

            @Override
            public void keyPressed(KeyEvent e) {
                //对按键按下以后 执行操作
                int keyCode = e.getKeyCode();
                switch (keyCode){
                    case 37:
                        System.out.println("左按键操作");
                        break;
                    case 38:
                        System.out.println("上按键操作");
                        break;
                    case 39:
                        System.out.println("右键操作");
                        break;
                    case 40:
                        System.out.println("下按键操作");
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                //对按键按下松开以后 执行操作


            }
        });


        frame.setVisible(true);
    }

}
